﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;

namespace AISFMIOperator
{
    public class SQLDBLayer
    {
        private static string ConnectionString = AppSettings.db_connection_string;
        private static SqlConnection connection;

        public static void OpenConnection()
        {
            connection = new SqlConnection(ConnectionString);
            connection.Open();
        }

        public static void CloseConnection()
        {
            connection.Close();
        }

        public static DataSet ExecSPTables(SqlCommand command)
        {
            command.Connection = connection;
            DataSet tables = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(command);
            sda.Fill(tables);
            return tables;
        }

        public static DataTable ExecSPTable(SqlCommand command)
        {
            command.Connection = connection;
            DataTable table = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter(command);
            sda.Fill(table);
            return table;
        }

        public static void ExecSPNonQuery(SqlCommand command)
        {
            command.Connection = connection;
            command.ExecuteNonQuery();
        }

        public static DataTable ExecQuery(string query)
        {
            SqlCommand command = new SqlCommand(query, connection);
            DataTable table = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter(command);
            sda.Fill(table);
            return table;
        }

        public SqlConnection Connection
        {
            get { return this.Connection; }
        }

        public static SqlDbType GetParameterTypeByName(string type_name)
        {
            SqlDbType sql_type = SqlDbType.Int;

            switch (type_name)
            {
                case "int":
                    sql_type = SqlDbType.Int;
                    break;
                case "varchar":
                    sql_type = SqlDbType.VarChar;
                    break;
                case "datatime":
                    sql_type = SqlDbType.DateTime;
                    break;
                case "float":
                    sql_type = SqlDbType.Float;
                    break;
                case "tinyint":
                    sql_type = SqlDbType.TinyInt;
                    break;
                case "bit":
                    sql_type = SqlDbType.Bit;
                    break;
            }

            return sql_type;
        }
    }
}
